package org.aaron.demos;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.aaron.demos.model.GeoCodingResult;
import org.aaron.demos.model.Result;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.RequestWrapper;

import com.google.gson.Gson;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class LocationActivity extends Activity {
	private LocationManager locationManager;
	private Button button1, button2, button3, button4, button5, button6, button7;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.locationlayout);
		initActivity();
	}

	private void initActivity() {
		// 获取当前位置，通过GPS提供者
		button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				locationManager = (LocationManager) LocationActivity.this.getSystemService(Context.LOCATION_SERVICE);
				locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationManagerListener());
			}
		});
		// 获取定位服务的提供者列表
		button2 = (Button) findViewById(R.id.button2);
		button2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				List<String> providers = locationManager.getAllProviders();
				for (String provider : providers) {
					Log.d("provider", provider);
				}
			}
		});

		// 获取根据查询条件
		button3 = (Button) findViewById(R.id.button3);
		button3.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Criteria criteria = new Criteria();
				criteria.setAccuracy(Criteria.ACCURACY_FINE);
				criteria.setPowerRequirement(Criteria.POWER_LOW);
				criteria.setAltitudeRequired(false);
				criteria.setSpeedRequired(false);
				criteria.setCostAllowed(false);
				String providet = locationManager.getBestProvider(criteria, true);
				Log.d("Best Provider:", providet);
			}
		});
		// GeoCoder获取经纬度
		button4 = (Button) findViewById(R.id.button4);
		button4.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				new GeoCoderTask().execute(null);
			}

		});
		// GeoCoder根据经纬度获取位置
		button5 = (Button) findViewById(R.id.button5);
		button5.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				new ReverseGeoCoderTask().execute(null);
			}

		});
		// GeoCoding Http请求根据经纬度获取位置
		button6 = (Button) findViewById(R.id.button6);
		button6.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String url = "http://maps.googleapis.com/maps/api/geocode/json?address=Winnetka&sensor=false";
				StringBuilder databuilder = new StringBuilder();
				HttpClient client = new DefaultHttpClient();
				try {
					HttpResponse httpResponse = client.execute(new HttpGet(url));
					HttpEntity httpEntity = httpResponse.getEntity();
					BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpEntity.getContent()));
					String line = "";
					while ((line = bufferedReader.readLine()) != null) {
						databuilder.append(line);
					}

				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				Gson gson = new Gson();
				GeoCodingResult codingResult = gson.fromJson(databuilder.toString(), GeoCodingResult.class);
				Log.d("GeoCodingResult", codingResult.toString());
			}
		});
		// GeoCoding Http请求根据经纬度获取位置
		button7 = (Button) findViewById(R.id.button7);
		button7.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=39.90,116.39&sensor=false";
				StringBuilder databuilder = new StringBuilder();
				HttpClient client = new DefaultHttpClient();
				try {
					HttpResponse httpResponse = client.execute(new HttpGet(url));
					HttpEntity httpEntity = httpResponse.getEntity();
					BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpEntity.getContent()));
					String line = "";
					while ((line = bufferedReader.readLine()) != null) {
						databuilder.append(line);
					}

				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				Gson gson = new Gson();
				GeoCodingResult codingResult = gson.fromJson(databuilder.toString(), GeoCodingResult.class);
				Log.d("GeoCodingResult", codingResult.toString());
			}
		});
	}

	class LocationManagerListener implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			Log.d("Location 经度", location.getLongitude() + "");
			Log.d("Location 纬度", location.getLatitude() + "");

		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

	}

	private class GeoCoderTask extends AsyncTask<Integer, Integer, Integer> {

		@Override
		protected Integer doInBackground(Integer... params) {
			Geocoder geocoder = new Geocoder(LocationActivity.this);
			try {
				List<Address> addresses = geocoder.getFromLocationName("SFO", 1);
				for (Iterator<Address> it = addresses.iterator(); it.hasNext();) {
					Log.d("Adress Latitude:", it.next().getLatitude() + "");
					Log.d("Adress Longitude:", it.next().getLongitude() + "");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

	}

	private class ReverseGeoCoderTask extends AsyncTask<Integer, Integer, Integer> {

		@Override
		protected Integer doInBackground(Integer... params) {
			Geocoder geocoder = new Geocoder(LocationActivity.this);
			try {
				List<Address> addresses = geocoder.getFromLocation(39.90071, 116.39, 1);
				for (Iterator<Address> it = addresses.iterator(); it.hasNext();) {
					Log.d("Adress Address:", it.next() + "");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

	}

}
